package com.irdstudio.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.service.facade.ContTempGService;
import com.irdstudio.efp.console.service.vo.ContTempGVO;
import com.irdstudio.efp.console.service.vo.ContTempRelGVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.ApplicationConstance;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class ContTempGController extends AbstractController  {
	
	@Autowired
	@Qualifier("contTempGService")
	private ContTempGService contTempGService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/cont/temp/gs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ContTempGVO>> queryContTempGAll(
			@RequestBody ContTempGVO vo) {		
		// 根据用户角色获取用户权限方案
		List<ContTempGVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = contTempGService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = contTempGService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = contTempGService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = contTempGService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/cont/temp/g/{contGroupId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<ContTempGVO> queryByPk(@PathVariable("contGroupId") String contGroupId) {		
		ContTempGVO inVo = new ContTempGVO();
				inVo.setContGroupId(contGroupId);
		ContTempGVO outputVo = contTempGService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param contTempG
	 * @return
	 */
	@RequestMapping(value="/cont/temp/g", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody ContTempGVO inContTempGVo) {		
		int outputVo = contTempGService.deleteByPk(inContTempGVo);
		if ( outputVo == -2 ) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage("只能删除待生效状态的合同模版");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}else
			return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inContTempGVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/g", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody ContTempGVO inContTempGVo) {		
		setUserInfoToVO(inContTempGVo);
		int outputVo = contTempGService.updateByPk(inContTempGVo);
		return getResponseData(outputVo);
		
	}
	
	@RequestMapping(value="/cont/temp/g/submit", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateSubmitByPk(@RequestBody ContTempGVO inContTempGVo) {		
		setUserInfoToVO(inContTempGVo);
		int outputVo = contTempGService.updateSubmitByPk(inContTempGVo);
		if (outputVo < 0 ) {
			String message;
			if(outputVo == -3) {
				message = "合同模版组未关联合同模版, 不可以生效!";
			}else {
				message = "处理失败!";
			}
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage(message);
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inContTempGVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/g", method=RequestMethod.POST)
	public @ResponseBody ResponseData<ContTempGVO> insertContTempG(@RequestBody ContTempGVO inContTempGVo) {
		setUserInfoToVO(inContTempGVo);
		ContTempGVO outputVo = contTempGService.insertContTempGBack(inContTempGVo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 失效合同模版组
	 * @param inContTempGVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/g/disable", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> disableContTempG(
			@RequestBody ContTempGVO inContTempGVo) {
		setUserInfoToVO(inContTempGVo);
		int outputVo = contTempGService.disableContTempG(inContTempGVo);
		if ( outputVo == -2) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage("已存在生效产品引用该合同模版组,操作失败!");
			responseData.setCode(outputVo + "");
			responseData.setSuccess(false);
			return responseData;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 新增合同模板组关联信息
	 * @param inContTempGVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/rel/g", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertContTempRelG(@RequestBody ContTempRelGVO inContTempRelGVO) {
		setUserInfoToVO(inContTempRelGVO);
		int outputVo = contTempGService.insertContTempRelG(inContTempRelGVO);
		if ( outputVo < 0) {
			String message;
			switch (outputVo) {
			case -2:
				message = "已存在合同模版组关联信息,操作失败!";
				break;
			case -3:
				message = "存在关联合同模版组的合同模版 模版种类, 合同类型, 是否最高额合同三种纬度相同的合同模板";
				break;
			default:
				message = "处理失败!";
				break;
			}
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage(message);
			responseData.setCode(outputVo + "");
			responseData.setSuccess(false);
			return responseData;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 删除合同模板组关联信息
	 * @param inContTempGVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/rel/g", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteContTempRelG(
			@RequestBody ContTempRelGVO inContTempRelGVO) {
		setUserInfoToVO(inContTempRelGVO);
		int outputVo = contTempGService.deleteContTempRelG(inContTempRelGVO);
		return getResponseData(outputVo);
	}
	
	/**
	 * 查询当前登录法人机构 生效的合同模版组
	 * @param inContTempRelGVO
	 * @return
	 */
	@RequestMapping(value="/cont/temp/g/allEnables", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ContTempGVO>> queryAllEnables(
			@RequestBody ContTempGVO inContTempGVO) {
		setUserInfoToVO(inContTempGVO);
		List<ContTempGVO> outputVo = contTempGService.queryAllEnables(inContTempGVO);
		return getResponseData(outputVo);
	}
}
